<?php

namespace common\models\ofc;

use Yii;
use common\helpers\Html;
use common\helpers\Url;

/**
 * This is the model class for table "{{%ofc_competition}}".
 *
 * @property int $id
 * @property string $name 赛事名称
 * @property string|null $name_en 赛事英文名称
 * @property string|null $abbr 赛事简称
 * @property string|null $abbr_en 英文简称
 * @property string|null $description 赛事简介
 * @property int $type 赛事类型:1联赛，2杯赛，3友谊赛，0其他未知
 * @property string|null $logo 赛事logo
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class OfcCompetition extends \common\db\ActiveRecord {

    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%ofc_competition}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['type', 'status', 'created_at', 'updated_at'], 'integer'],
            [['name', 'name_en', 'abbr_en'], 'string', 'max' => 50],
            [['abbr'], 'string', 'max' => 30],
            [['description', 'logo'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '赛事名称',
            'name_en' => '赛事英文名称',
            'abbr' => '赛事简称',
            'abbr_en' => '英文简称',
            'description' => '赛事简介',
            'type' => '赛事类型:1联赛，2杯赛，3友谊赛，0其他未知',
            'logo' => '赛事logo',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public function getCurrentSeason() {
        return $this->hasOne(OfcSeason::className(), ['competition_id' => 'id'])->andWhere([
            'current' => 1
        ]);
    }

    public function getLabel() {
        $classlist = [
            'label-default',
            'label-success',
            'label-info',
            'label-danger',
        ];
        $class = isset($classlist[$this->id]) ? $classlist[$this->id] : 'label-default';
        return Html::label($this->abbr, null, ['class' => 'label label-sm ' . $class]);
    }

    public function getTypeList() {
        return [
            1 => '联赛',
            2 => '杯赛',
            3 => '友谊赛',
            0 => '其他赛事',
        ];
    }

    public function getBrand() {
        return Url::attach($this->logo);
    }

    /**
     * @inheritdoc
     */
    public function fields() {
        return [
            'id',
            'name',
            'name_en',
            'abbr',
            'abbr_en',
            'description',
            'brand',
        ];
    }

}
